//
//  CPUIButtonUtil.swift
//  BabaLink
//
//  Created by wrs on 2018/7/30.
//  Copyright © 2018年 wrs. All rights reserved.
//

import Foundation
import UIKit

private var topKey: Void?
private var bottomKey: Void?
private var rightKey: Void?
private var leftKey: Void?

private var originTitleKey: Void?

extension UIButton {
 
   public func setEnlargeEdge(top: Float, right: Float, bottom: Float, left: Float) -> Void {
        objc_setAssociatedObject(self, &topKey, NSNumber(value: top), .OBJC_ASSOCIATION_COPY)
        objc_setAssociatedObject(self, &rightKey, NSNumber(value: right), .OBJC_ASSOCIATION_COPY)
        objc_setAssociatedObject(self, &bottomKey, NSNumber(value: bottom), .OBJC_ASSOCIATION_COPY)
        objc_setAssociatedObject(self, &leftKey, NSNumber(value: left), .OBJC_ASSOCIATION_COPY)
    }
    
    open override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        let rect = self.enlargedRect()
        if rect == self.bounds {
            return super.point(inside: point, with: event)
        } else {
           return rect.contains(point)
        }
    }
    
    
    func enlargedRect() -> CGRect {
        let topEdge: NSNumber? = objc_getAssociatedObject(self, &topKey) as? NSNumber
        let rightEdge: NSNumber? = objc_getAssociatedObject(self, &rightKey) as? NSNumber
        let bottomEdge: NSNumber? = objc_getAssociatedObject(self, &bottomKey) as? NSNumber
        let leftEdge: NSNumber? = objc_getAssociatedObject(self, &leftKey) as? NSNumber
        if topEdge != nil && rightEdge != nil && bottomEdge != nil && leftEdge != nil {
            let x = Float(self.bounds.origin.x) - leftEdge!.floatValue
            let y = Float(self.bounds.origin.y) - topEdge!.floatValue
            let width = Float(self.bounds.size.width) + leftEdge!.floatValue + rightEdge!.floatValue
            let height = Float(self.bounds.size.height) + topEdge!.floatValue + bottomEdge!.floatValue
            return CGRect(x: CGFloat(x), y: CGFloat(y), width: CGFloat(width), height: CGFloat(height))
        } else {
            return self.bounds
        }
    }
    
    func setCountDown(time: Int, prefix: String, suffix: String, enable: Bool) -> Void {
        let originTitle = self.titleLabel?.text
        objc_setAssociatedObject(self, &originTitleKey, originTitle, .OBJC_ASSOCIATION_COPY)
        self.countDown(time: time, prefix: prefix, suffix: suffix, enable: enable)
    }
    
    
    
    func countDown(time: Int, prefix: String, suffix: String, enable: Bool) -> Void {
        var remainTime: Int = time
        let zero: Int = 0
        let title = "\(prefix)\(remainTime)\(suffix)"
        self.setCountTimeTitle(title: title, enable: enable)
        DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
            remainTime = remainTime - 1
            
            if remainTime >= zero {
                self.countDown(time: remainTime, prefix: prefix, suffix: suffix, enable: enable)
            } else {
                let title = objc_getAssociatedObject(self, &originTitleKey) as? String
                self.setCountTimeTitle(title: title, enable: true)
            }
        }
    }
    
    func setCountTimeTitle(title: String?, enable: Bool) -> Void {
        self.isEnabled = enable
        self.titleLabel?.text = title
        self.setTitle(title, for: UIControl.State.normal)
    }
}
